package com.hmall.user.servicr.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.user.mapper.UserMapper;
import com.hmall.user.pojo.Address;
import com.hmall.user.pojo.User;
import com.hmall.user.servicr.UserService;
import com.hmall.user.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 29143
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, Address> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户信息查询地址列表
     * 返回一个list集合
     * @param
     * @return
     */
    @Override
    public List<Address> findAddressListById() {
        Long userId = ThreadLocalUtils.getUserId();
        System.out.println(userId);
        List<Address> list = userMapper.findAddressListById(userId);
        return list;
    }

    /**
     * 根据addressId查询地址信息
     * @param addressId
     * @return
     */
    @Override
    public Address findAddressById(Long addressId) {
        Address address = userMapper.selectById(addressId);
        return address;
    }

    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @Override
    public User findUserById(Long userId) {
        return userMapper.findUserById(userId);
    }

    /**
     * 余额支付
     * @param userId
     * @param money
     */
    @Override
    @Transactional
    public void payMoney(Long userId, Long money) {
        Integer result = userMapper.payMoney(userId,money);
        if (result==0){
            throw new RuntimeException("支付失败");
        }
    }
}
